<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use think\Db;

/**
 * 会员管理
 * Class AdminUser
 * @package app\admin\controller
 */
class Member extends AdminBase {

    //定义数据表
    private $db;

    // 构造函数 实例化Red表
    public function __construct() {
        parent::__construct();
        $this->db = Db::name('Member');
    }

    /**
     * 会员管理
     * @return mixed
     */
    public function index() {
        $where = 1;
        if ($account = input('get.account'))
            $where .= ' and account like "%' . $account . '%"';
        if ($certificate_code = input('get.certificate_code'))
            $where .= ' and certificate_code like "%' . $certificate_code . '%"';
        if ($phone = input('get.phone'))
            $where .= ' and phone like "%' . $phone . '%"';
        if ($email = input('get.email'))
            $where .= ' and email like "%' . $email . '%"';

        //获取列表
        $data = $this->db->where($where)->order('id desc')->paginate(10);

        // 获取分页显示
        $page = $data->render();

        // 模板变量赋值
        $this->assign(array(
            'data' => $data,
            'page' => $page
        ));

        return $this->fetch();
    }

    //会员添加
    public function add() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result = $this->validate($data, 'Member.add');

            if (true !== $result) {
                $this->error($result);
            } else {
                $data['password'] = md5($data['password']);
                $data['add_time'] = time();
                $this->db->insert($data);
                $this->success('会员添加成功');
            }
        }
        $this->assign('member_level', cache('member_level'));
        return $this->fetch();
    }

    //会员编辑
    public function edit() {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            if ($data['password']) {
                if (strlen($data['password']) > 0 && strlen($data['password']) < 6) {
                    $this->error('会员密码不能少于6位!');
                } else {
                    $data['password'] = md5($data['password']);
                }
            } else {
                unset($data['password']);
            }

            $result = $this->validate($data, 'Member.edit');

            if (true !== $result) {
                $this->error($result);
            } else {
                $this->db->update($data, ['id' => $data['id']]);
                $this->success('会员修改成功');
            }
        }

        $id = input('param.id');
        $data = $this->db->find($id);
        $this->assign('member_level', cache('member_level'));
        return $this->fetch('', ['data' => $data]);
    }

    public function after() {
        $member_id = input('param.member_id');

        $data = Db::name('memberEduInfo')->where(['member_id' => $member_id])->paginate(10);

        // 获取分页显示
        $page = $data->render();

        // 模板变量赋值
        $this->assign(array(
            'data' => $data,
            'page' => $page
        ));

        return $this->fetch();
    }

    /**
     * 删除会员
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = []) {
        $id = $ids ? $ids : $id;
        if ($id) {
            if ($this->db->delete($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的会员');
        }
    }

    /**
     * 导入会员
     * @return type
     */
    public function import() {
        if ($this->request->isPost()) {
            set_time_limit(0);
            $post = input('post.');

            if (!$post['exel'])
                $this->error('请上传exel文件');
            try {
                $res = import_excel(trim($post['exel'], '/'));
            } catch (Exception $ex) {
                $this->error('exel格式有错误');
            }


            foreach ($res as $k => $v) {
                if ($k != 1) {  //跳过标题
                    $keys = [
                        'account' => $v[0] ? $v[0] : '',
                        'password' => $v[1] ? md5($v[1]) : '',
                        'prefix' => $v[2] ? $v[2] : '',
                        'first_name' => $v[3] ? $v[3] : '',
                        'middle_name' => $v[4] ? $v[4] : '',
                        'surnames' => $v[5] ? $v[5] : '',
                        'date_of_birth' => $v[6] ? $v[6] : '',
                        'organization' => $v[7] ? $v[7] : '',
                        'job_title' => $v[8] ? $v[8] : '',
                        'phone' => $v[9] ? $v[9] : '',
                        'fax' => $v[10] ? $v[10] : '',
                        'email' => $v[11] ? $v[11] : '',
                        'contact_by' => $v[12] ? $v[12] : '',
                        'level_status' => $v[13],
                        'education_status' => $v[14],
                        'exam_registration' => $v[15],
                        'work_experience' => $v[16],
                        'certificate_code' => $v[17],
                        'default_addr' => $v[18],
                        'country' => $v[19] ? $v[19] : 0,
                        'city' => $v[20] ? $v[20] : '',
                        'level_id' => $v[21] ? $v[21] : 1,
                        'pay_status' => $v[22] ? $v[22] : 0,
                        'add_time' => time(),
                    ];

                    if (!Db::name('Member')->where(['account' => $keys['account']])->find()) {
                        if (isset($keys) && $keys) {
                            Db::name('Member')->insert(array_filter($keys));
                        }
                    }
                }
            }

            $this->success('导入成功！');
        }

        return $this->fetch('');
    }

}
